<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends CommonController {

    //列表
    public function lists(){

         // $list=D('Banner')->join('my_banner_point on my_banner.point_id = my_banner_point.id ')->field('my_banner.*,my_banner_point.name')->where($where)->select();
         $a[]='brand';
         $a[]='cate';
        $list=D('goods')->relation(true)->select();
        //print_r($list);die;
        $this->assign('list',$list);
        $this->display();
    }
    public function add(){
        //s('spec_item',null);
        if(IS_POST){
            if(!empty($_FILES['img']['name'])){
                $img=$this->upload();
            }
            $_POST['img']=$img;
            unset($_POST['stock']);
            unset($_POST['price']);
            $_POST['goods_no']=date('Ymd',time()).mt_rand(1000,9999);
            $id=D('goods')->add(i('post.'));
            //print_r($id);die;

             if($id){
                foreach (S('spec_item') as $k => $v) {
                    # code...
                    $v['spec_rule']=$id.$v['spec_rule'];
                    $v['goods_id']=$id;
                    $g_id=D('spec_price')->add($v);
                }
                s('spec_item',null);
            $this->success('新增成功', 'lists');die;
            } else {    //错误页面的默认跳转页面是返回前一页，通常不需要设置
                $this->error('新增失败');}die;

        }

        $spec_list=D('spec')->where('g_type_id=3')->relation(true)->select();
        $spec_item_list=D('spec_item')->select();
        $goods_type=D('goods_type')->select();
        $brand_list=D('brand')->select();
        $cate_list=D('cate')->select();
// print_r($spec_item_list);die;
        $this->assign('spec_list',$spec_list);
        $this->assign('spec_item_list',$spec_item_list);
        $this->assign('cate_list',$cate_list);
        $this->assign('goods_type',$goods_type);
        $this->assign('brand_list',$brand_list);
        $this->display();
    }
    public function goods_img(){
        if(IS_POST){
             if(!empty($_FILES['files']['name'])){

                $img=$this->upload();
            }
            $data['url']=$img;
            $data['goods_id']=i('post.goods_id');
            D('goods_img')->add($data);

        }
        $id=i('get.id');
        $list=D('goods_img')->where("goods_id=$id")->relation(true)->select();
        $this->assign('list',$list);
        $this->display();
    }

}